package com.bruce.langchain4j.modelscope.autoConfigure;

import java.time.Duration;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.bruce.langchain4j.modelscope.ModelScopeProperties;
import com.bruce.langchain4j.modelscope.service.ModelScopeService;
import com.bruce.langchain4j.modelscope.service.impl.ModelScopeServiceImpl;

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;

@AutoConfiguration
@EnableConfigurationProperties(ModelScopeProperties.class)
public class ModelScopeAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean(name = "modelScopeChatModel")
    public ChatLanguageModel modelScopeChatModel(ModelScopeProperties modelScopeProperties) {
        return OpenAiChatModel.builder()
               .apiKey(modelScopeProperties.getApiKey())
               .baseUrl(modelScopeProperties.getBaseUrl())
               .modelName(modelScopeProperties.getModelName())
               .temperature(modelScopeProperties.getTemperature())
               .maxTokens(modelScopeProperties.getMaxTokens())
               .timeout(Duration.ofMillis(modelScopeProperties.getTimeOut()))
               .logRequests(modelScopeProperties.getLogRequests())
               .logResponses(modelScopeProperties.getLogResponses())
               .build();
    }


    @Bean
    @ConditionalOnMissingBean
    public ModelScopeService modelScopeService(ChatLanguageModel modelScopeChatModel) {
        return new ModelScopeServiceImpl(modelScopeChatModel);
    }

}
